<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;


use App\Models\Admin\VshopsWorker;
use App\Models\Admin\TnetReginfo;

class VshopsWorkerController extends CommonController
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index(Request $request)
	{
	    //var_dump(__METHOD__); exit();
	    
	    $data = VshopsWorker::where(array('merchant_id' => $this->merchant_id))->get();
	    //var_dump($data); exit();
		return view('admin.vshops_worker.index')->with(['data' => $data]);
	}

	/**
	 * 员工图添加/修改
	 */
	public function info(Request $request)
	{
	    //var_dump(__METHOD__); exit();
	    
		$id = (int)$request->input('id', 0);

		if($id){
			$data = VshopsWorker::where(array('merchant_id' => $this->merchant_id, 'id' => $id))->first();
		}else{
		    $data = new \stdClass();
		    $data->id = $id;
		}
		
		return view('admin.vshops_worker.info')->with(['data' => $data]);
	}
	
	public function save(Request $request)
	{
	    //var_dump(__METHOD__); exit();
	    
	    $id = intval($request->input('id', 0));
	    $worker_name = $request->input('worker_name');
	    $worker_desc = $request->input('worker_desc');
	    $nodecode = $request->input('nodecode');
	    $profit_percent = $request->input('profit_percent');
	    //var_dump($profit_percent); exit();
	    $status = intval($request->input('status'));
	    
	    if(empty($worker_name)){
	        $this->ajax_error('请输入员工名称');
	    }
	    if(empty($worker_desc)){
	        $this->ajax_error('请输入描述');
	    }
	    if(empty($nodecode)){
	        $this->ajax_error('请输入手机');
	    }
	    if(empty($profit_percent)){
	        $this->ajax_error('请输入分润百分比');
	    }
	    if( empty($status) || (!in_array($status, array(1, 2))) ){
	        $this->ajax_error('请选择状态');
	    }
	    
	    //检查手机是否已注册
	    $user_info = TnetReginfo::where(array('nodecode' => $nodecode))->first();
	    //var_dump($worker_user); exit();
	    if(empty($user_info)){
	        $this->ajax_error('该手机还未注册，请先注册');
	    }	    
	    
	    //所有员工百分比总和不能大于100
	    $where = array('merchant_id' => $this->merchant_id);
	    if($id){
	         $where[] = ['id', '<>', $id];
	    }
	    $profit_percent_sum  = VshopsWorker::where($where)->sum('profit_percent');
	    //var_dump($profit_percent_sum); exit();
	    $profit_percent_sum += $profit_percent;
	    if($profit_percent_sum > 100){
	        $this->ajax_error('所有员工百分比总和不能大于100');
	    }
	    
	    //开始新增、修改
	    $data = array(
	        'worker_name'	  => $worker_name,
	        'worker_desc'	  => $worker_desc,
	        'nodecode'		  => $nodecode,
	        'profit_percent'  => $profit_percent,
	        'status'		  => $status,
	        'nodeid'		  => $user_info->nodeid,
	        //'merchant_id'	=> $this->merchant_id,
	    );
	    
	    if($id){
	        if(!VshopsWorker::where(array('merchant_id' => $this->merchant_id, 'id' => $id))->update($data)) $this->ajax_error('连接失败，请重新提交');
	    }else{
	        $data['id'] = VshopsWorker::getNextSeq();
	        $data['merchant_id'] = $this->merchant_id;
	        $data['add_time'] = date('Y-m-d H:i:s');
	        if(!VshopsWorker::insert($data)) $this->ajax_error('连接失败，请重新提交');
	    }
	    
	    $this->ajax_success('提交成功');
	}

	/**
	 * 删除员工
	 */
	public function del(Request $request)
	{
		$id = $request->input('id', 0);
		if(!$id) $this->ajax_error('操作失败');

		if(!VshopsWorker::where(array('merchant_id' => $this->merchant_id, 'id' => $id))->delete()) $this->ajax_error('操作失败，请重试');
		$this->ajax_success('删除成功');		
	}


}